// 显示隐藏高级功能
function aidrawGj(btn){
    if ($("#aidrawDiv").is(':hidden')) {
        $("#aidrawDiv").show();
        $(btn).html('高级功能 ∧');
    }else{
        $("#aidrawDiv").hide();
        $(btn).html('高级功能 ∨');
    }
}

// 发送绘画指令
function sendAiDraw(){
    var params = new FormData();

    // var files = $("#htsc_file")[0].files;
    // params.append('file',files[0]);
    params.append('prompt',$("#aidraw_prompt").val());
    params.append('chicun',$("#aidraw_chicun").val());
    params.append('num',$("#aidraw_num").val());
    params.append('session_sn',global_assistant.session_sn);

    var l = $('body').lyearloading({
        opacity: 0.5,
        spinnerSize: 'lg',
        spinnerText: '请求发送中，请稍后',
        textColorClass: 'text-info',
        spinnerColorClass: 'text-info'
    });
    
    $.ajax({
        url:api_path+'msg/aiDraw',
        data:params,
        type:'post',
        contentType:false,
        processData:false,
        success:function(response){
          var data = response.data;
          if (response.code == '2000' || response.code == '2001') {
            var str = createAidrawMessageHtml(data.message,data.ctime);
            str += createMsgHtml(data.reply,1,data.rtime);

            $("#chatContent").append(str);
            $("#aiDraw").modal('hide');

            if (response.code == '2000') {
                global_wait_msgs.push(response.msg_id);
            }
            scrollBottom();
          }else{
            cchUtil.message(response.msg,'danger');
          }
          
          l.hide();
        }
    });
}

// 生成对话HTML
function createAidrawMessageHtml(message,ctime){
    if (ctime == null || ctime == '') {
        ctime = getFamtterHours();
    }
    var msg_class = 'message self';
    var avatar = upload_path+'/'+global_user.headimgurl;

    var str = '<div class="'+msg_class+'">'+
                '<div class="message-wrapper">'+
                    '<div class="message-content">'+
                        '<h6>AI作画</h6>'+
                        '<h6>生图的文本描述：'+message.prompt+'</h6>'+
                        '<h6>尺寸：'+message.chicun+'</h6>'+
                        '<h6>数量：'+message.num+'</h6>'+
                        // '<a class="popup-media" href="'+static_path+'/index/htsc_file.png">'+
                        //     '<img class="img-fluid rounded" src="'+static_path+'/index/htsc_file.png" alt="" style="width:100px;">'+
                        // '</a>'+
                    '</div>'+
                    
                '</div>'+
                '<div class="message-options">'+
                    '<div class="avatar avatar-sm"><img alt="" src="'+avatar+'"></div>'+
                '</div>'+
            '</div>';

    return str;
}

// 生成对话HTML
function createAidrawReplyHtml(message,ctime){
    console.log(message)
    if (message == null || message == '') {
      return '';
    }
    if (ctime == null || ctime == '') {
        ctime = getFamtterHours();
    }
    var msg_class = 'message';
        var avatar = upload_path+'/'+global_assistant.image;

    var imgstr = '';
    for(var i in message){
        imgstr += '<a class="popup-media" href="'+message[i]+'" style="float:left;padding:5px 4px;">'+
                            '<img class="img-fluid rounded" src="'+message[i]+'" alt="" style="width:100px;">'+
                        '</a>';
    }

    var str = '<div class="'+msg_class+'">'+
                '<div class="message-wrapper">'+
                    '<div class="message-content">'+imgstr+'</div>'+
                '</div>'+
                '<div class="message-options">'+
                    '<div class="avatar avatar-sm"><img alt="" src="'+avatar+'"></div>'+
                    '<span class="message-date">'+ctime+'</span>'+
                '</div>'+
            '</div>';
            console.log(str)

    return str;
}